/**
 * ExtraLifePowerUp - 额外生命道具
 * 为玩家增加额外的生命值
 */

import { PowerUp, PowerUpType, PowerUpRarity, type PowerUpConfig, type PowerUpEffect, type PowerUpOptions } from '../PowerUp'
import type { Player } from '../Player'

/**
 * 额外生命效果实现
 */
export class ExtraLifeEffect implements PowerUpEffect {
  private lifeAmount: number = 1

  /**
   * 应用额外生命效果
   */
  apply(player: Player): void {
    // 直接增加玩家生命值
    player.heal(this.lifeAmount)
    
    console.log(`额外生命效果已激活，增加 ${this.lifeAmount} 点生命值`)
  }

  /**
   * 移除额外生命效果
   */
  remove(_player: Player): void {
    // 额外生命是永久性的，不需要移除
    console.log('额外生命效果已应用（永久性）')
  }

  /**
   * 更新额外生命效果
   */
  update(_player: Player, _deltaTime: number): void {
    // 额外生命是瞬时效果，不需要每帧更新
  }

  /**
   * 获取生命增加量
   */
  getLifeAmount(): number {
    return this.lifeAmount
  }

  /**
   * 设置生命增加量
   */
  setLifeAmount(amount: number): void {
    this.lifeAmount = Math.max(1, amount)
  }
}

/**
 * 额外生命道具类
 */
export class ExtraLifePowerUp extends PowerUp {
  constructor(options: Omit<PowerUpOptions, 'powerUpType'>) {
    super({
      ...options,
      powerUpType: PowerUpType.EXTRA_LIFE
    })
  }

  /**
   * 创建额外生命道具配置
   */
  protected createConfig(customConfig?: Partial<PowerUpConfig>): PowerUpConfig {
    const defaultConfig: PowerUpConfig = {
      type: PowerUpType.EXTRA_LIFE,
      duration: 0, // 瞬时效果
      value: 1, // 增加的生命值
      rarity: PowerUpRarity.RARE,
      stackable: true,
      maxStacks: 5,
      description: '增加一点生命值',
      iconColor: '#FF5722' // 红色
    }

    return { ...defaultConfig, ...customConfig }
  }

  /**
   * 创建额外生命效果
   */
  protected createDefaultEffect(): PowerUpEffect {
    return new ExtraLifeEffect()
  }

  /**
   * 渲染额外生命道具的特殊效果
   */
  protected renderDefault(ctx: CanvasRenderingContext2D): void {
    // 绘制心形图标
    ctx.fillStyle = this.config.iconColor
    
    const centerX = 0
    const centerY = 0
    const size = Math.min(this.width, this.height) * 0.6
    
    // 绘制心形
    ctx.beginPath()
    const topCurveHeight = size * 0.3
    
    // 左半心
    ctx.moveTo(centerX, centerY + size * 0.2)
    ctx.bezierCurveTo(
      centerX, centerY - topCurveHeight,
      centerX - size * 0.5, centerY - topCurveHeight,
      centerX - size * 0.5, centerY
    )
    ctx.bezierCurveTo(
      centerX - size * 0.5, centerY + topCurveHeight * 0.5,
      centerX, centerY + topCurveHeight * 0.5,
      centerX, centerY + size * 0.5
    )
    
    // 右半心
    ctx.bezierCurveTo(
      centerX, centerY + topCurveHeight * 0.5,
      centerX + size * 0.5, centerY + topCurveHeight * 0.5,
      centerX + size * 0.5, centerY
    )
    ctx.bezierCurveTo(
      centerX + size * 0.5, centerY - topCurveHeight,
      centerX, centerY - topCurveHeight,
      centerX, centerY + size * 0.2
    )
    
    ctx.closePath()
    ctx.fill()

    // 绘制高光效果
    ctx.fillStyle = '#FFCDD2'
    ctx.beginPath()
    ctx.ellipse(
      centerX - size * 0.2, 
      centerY - size * 0.1, 
      size * 0.15, 
      size * 0.1, 
      0, 0, Math.PI * 2
    )
    ctx.fill()

    // 绘制"+"符号表示增加
    ctx.strokeStyle = '#FFFFFF'
    ctx.lineWidth = 2
    ctx.beginPath()
    ctx.moveTo(centerX + size * 0.3, centerY - size * 0.1)
    ctx.lineTo(centerX + size * 0.3, centerY + size * 0.1)
    ctx.moveTo(centerX + size * 0.2, centerY)
    ctx.lineTo(centerX + size * 0.4, centerY)
    ctx.stroke()
  }
}